package com.atguigu.gmall.publisher.controller;

import com.atguigu.gmall.publisher.bean.TradeProvinceOrderAmount;
import com.atguigu.gmall.publisher.service.TradeStatsService;
import com.atguigu.gmall.publisher.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/gmall/realtime/trade")
public class TradeController {

    @Autowired
    private TradeStatsService tradeStatsService;

    @RequestMapping("/provinceOrderAmount")
    public String getProvinceOrderAmount(
            @RequestParam(value = "date", defaultValue = "1") Integer date) {
        if (date == 1) {
            date = DateUtil.now();
        }
        List<TradeProvinceOrderAmount> tradeProvinceOrderAmountList = tradeStatsService.getTradeProvinceOrderAmount(date);
        if (tradeProvinceOrderAmountList == null) {
            return "";
        }
        StringBuilder mapData = new StringBuilder("[");
        for (int i = 0; i < tradeProvinceOrderAmountList.size(); i++) {
            TradeProvinceOrderAmount tradeProvinceOrderAmount = tradeProvinceOrderAmountList.get(i);
            String provinceName = tradeProvinceOrderAmount.getProvinceName();
            Double orderAmount = tradeProvinceOrderAmount.getOrderAmount();
            Double orderCount = tradeProvinceOrderAmount.getOrderCount();
            Double userCount = tradeProvinceOrderAmount.getUserCount();
            mapData.append("{\n" +
                    "  \"name\":\""+provinceName+"\",\n" +
                    "  \"value\":"+orderAmount+",\n" +
                    "  \"tooltipValues\":[\n" +
                    "  "+orderCount+",\n" +
                    "  "+userCount+"\n" +
                    "  ]\n" +
                    "  \n" +
                    "}");
            if (i < tradeProvinceOrderAmountList.size() - 1) {
                mapData.append(",");
            } else {
                mapData.append("]");
            }
        }

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"mapData\": " + mapData + ",\n" +
                "\"valueName\": \"订单总额\","+
                "\"tooltipNames\": [\n" +
                "      \"下单次数\",\n" +
                "      \"下单人数\"\n" +
                "    ],\n" +
                "    \"tooltipUnits\": [\n" +
                "      \"次\",\n" +
                "      \"次\"\n" +
                "    ]" +
                "  }\n" +
                "}";
    }
}
